<template>
  <div class="app-container">
    <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键词">
        <el-input clearable v-model="searchForm.keyword" placeholder="搜索"></el-input>
      </el-form-item>
      <el-form-item label="日期范围">
        <el-date-picker
          v-model="searchForm.date_range"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
          value-format="yyyy-MM-dd"
          size="small"
          style="width: 300px"
        ></el-date-picker>

      </el-form-item>


      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      stripe
      highlight-current-row
    >
      <el-table-column align="center" label="#" width="100">
        <template v-slot:default="{scope,row}">
          {{ row.id }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="日期" width="150">
        <template v-slot:default="{scope,row}">
          {{ row.date }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="注册用户数" width="130">
        <template v-slot:default="{scope,row}">
          {{ row.reg_user_count }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="活跃用户数" width="130">
        <template v-slot:default="{scope,row}">
          {{ row.active_user_count }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="总记账笔数" width="130">
        <template v-slot:default="{scope,row}">
          {{ row.amount_count }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="总记账金额" width="130">
        <template v-slot:default="{scope,row}">
          {{ row.amount_total }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="收入总额" width="130">
        <template v-slot:default="{scope,row}">
          {{ row.amount_in }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="支出总额" width="130">
        <template v-slot:default="{scope,row}">
          {{ row.amount_out }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="接口访问次数" width="150">
        <template v-slot:default="{scope,row}">
          {{ row.api_count }}
        </template>
      </el-table-column>

      <el-table-column label="统计时间" width="180" align="center">
        <template v-slot:default="{scope,row}">
          <span>{{ row.created_at }}</span>
        </template>
      </el-table-column>

    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="fetchData"
    />
  </div>

</template>

<script>
import api from '@/api/sys_statistics'
import Pagination from '@/components/Pagination'

export default {
  components: {
    Pagination
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        '1': 'success',
        '0': 'gray',
        '-1': 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        sort: ''
      },
      searchForm: {
        keyword: '',
        date_range: []
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleCategorySelect(category) {
      this.searchForm.category_id = category
    },
    fetchData() {
      this.listLoading = true
      api.getList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    search() {
      this.listQuery.page = 1
      this.listQuery = Object.assign(this.listQuery, this.searchForm)
      this.fetchData()
    }
  }

}
</script>
